package com.xquant.common.engine.xPP.request;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.HashMap;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Instrument {
    private String id;
    private JSONObject termsheet;
    private String fpml;
    private String instrumentId;
    private String productType;
    private String initProductType;


    public String getInitProductType() {
        return initProductType;
    }

    public void setInitProductType(String initProductType) {
        this.initProductType = initProductType;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public JSONObject getTermsheet() {
        return termsheet;
    }

    public void setTermsheet(JSONObject termsheet) {
        this.termsheet = termsheet;
    }

    public String getFpml() {
        return fpml;
    }

    public void setFpml(String fpml) {
        this.fpml = fpml;
    }

    public String getInstrumentId() {
        return instrumentId;
    }

    public void setInstrumentId(String instrumentId) {
        this.instrumentId = instrumentId;
    }

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public Map<String, Instrument> split() {
        Map<String, Instrument> ret = new HashMap<>();
        if (fpml != null) {
            return null;
        } else {
            String json = this.getTermsheet().toJSONString();
            JSONArray legs = this.getTermsheet().getJSONArray("legs");
            JSONArray customLegs = this.getTermsheet().getJSONArray("customLegs");
            for (int i = 0; i < legs.size(); i++) {
                Instrument item = new Instrument();
                JSONObject leg = legs.getJSONObject(i);
                String legId = leg.getString("legId");
                item.setProductType(this.productType);
                item.setInitProductType(this.initProductType);
                for (int k = 0; k < customLegs.size(); k++) {
                    if (legId.equals(customLegs.getJSONObject(k).getString("legId"))) {
                        item.setProductType(customLegs.getJSONObject(k).getString("productType"));
                        break;
                    }
                }
                item.setId(this.id + legId);
                JSONObject obj = JSON.parseObject(json);
                JSONArray legArr = obj.getJSONArray("legs");
                for (int j = legArr.size() - 1; j >= 0; j--) {
                    JSONObject o = legArr.getJSONObject(j);
                    if (!legId.equals(o.getString("legId"))) {
                        legArr.remove(o);
                    }
                }
                JSONArray customLegArr = obj.getJSONArray("customLegs");
                for (int j = customLegArr.size() - 1; j >= 0; j--) {
                    JSONObject o = customLegArr.getJSONObject(j);
                    //如果存在 realnation   ,需要修改名义本金
                    if (o.get("realnotional") != null && o.get("realnotional") != "") {
                        obj.put("notional", o.get("realnotional"));
                    }
                    if (!legId.equals(o.getString("legId"))) {
                        customLegArr.remove(o);
                    }
                }
                item.setTermsheet(obj);
                ret.put(legId, item);
            }
        }
        return ret;
    }
}
